package org.example.chapter9.controller;

import org.example.chapter7.pojo.User;
import org.example.chapter9.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author CoderXshuai
 * @date 2020/8/9/0009 17:40
 */
@Controller
@RequestMapping("/user")
public class UserController {
    //注入用户服务类
    @Autowired
    private UserService userService;

    //展示用户详情
    @RequestMapping("details")
    public ModelAndView details(Long id) {
        //访问模型层得到数据
        User user = userService.getUser(id);
        //模型和视图
        ModelAndView mv = new ModelAndView();
        //定义模型视图
        mv.setViewName("user/details");
        //加入数据模型
        mv.addObject("user", user);
        //返回模型和视图
        return mv;
    }
}
